package com.chen.palmar.project.producer;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.chen.palmar.R;
import com.chen.palmar.base.BaseFragment;
import com.chen.palmar.common.constant.AppConstant;
import com.chen.palmar.common.datasource.DataCenter;
import com.chen.palmar.common.override.HttpSubscriber;
import com.chen.palmar.entity.ProducerStoreEntity;
import com.primb.androidlibs.ui.recyclerview.BaseQuickAdapter;
import com.primb.androidlibs.ui.recyclerview.BaseViewHolder;
import com.squareup.picasso.Picasso;

import java.util.HashMap;

import butterknife.Bind;
import rx.Subscription;

/**
 * Created by chen on 2018/1/6.
 */

public class StoreInfoFragment extends BaseFragment {

    @Bind(R.id.rv_store)
    RecyclerView rvStore;
    private BaseQuickAdapter<ProducerStoreEntity.DataBean, BaseViewHolder> adapter;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_store;
    }

    @Override
    public void initData() {
        rvStore.setNestedScrollingEnabled(false);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvStore.setLayoutManager(gridLayoutManager);
        rvStore.setItemAnimator(new DefaultItemAnimator());
        adapter = new BaseQuickAdapter<ProducerStoreEntity.DataBean, BaseViewHolder>(R.layout.item_producer_store) {
            @Override
            protected void convert(BaseViewHolder helper, ProducerStoreEntity.DataBean item) {
                helper.setText(R.id.tv_item_new_name, item.getTitle());
                helper.setText(R.id.tv_item_new_price, item.getGuidance_price() + "元");
                ImageView view = (ImageView) helper.getView(R.id.iv_item_new);
                Picasso.with(getContext()).load(item.getImg()).resize(AppConstant.SCREEN_WIDTH / 2- 50, AppConstant.SCREEN_WIDTH / 2 -80).placeholder(R.mipmap.occupy_icon).error(R.mipmap.occupy_icon).into(view);
            }
        };
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            Intent intent = new Intent(getContext(), StoreDetailActivity.class);
            intent.putExtra("info", adapter.getData().get(position).getProductId() + "");
            startActivity(intent);
        });
        rvStore.setAdapter(adapter);

        try {
            getDataForWeb(getArguments().getString("info"));
        } catch (Exception e) {
            e.printStackTrace();
            showToast("服务器数据出错，请稍后重试");
        }
    }

    private void getDataForWeb(String info) throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        map.put("factoryId", info);
        Subscription subscribe = DataCenter.producerStoreInfo(map).subscribe(new HttpSubscriber<ProducerStoreEntity>(getContext(), null) {
            @Override
            public void onNext(ProducerStoreEntity listEntity) {
                adapter.setNewData(listEntity.getData());
            }
        });
        subscription.add(subscribe);
    }
}
